import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { ConfigService } from '../../core/common/config.service';
@Injectable()
export class MenuService {

  menuItems: Array<any>;
  service = 'MenuService';
  constructor(public http: Http, private config: ConfigService) {
    this.menuItems = [];
  }

  getMenu() {
    return this.menuItems;
  }
  getMenus() {
    const method = 'getMenuList';
    const url = this.config.get('url') + '?service=' + this.service + '&method=' + method;
    const promise = this.http.post(url, null).toPromise().then((resp: Response) => {
      const result = resp.json();
      if (result.code === '0000') {
        const menus = [];
        result.info.forEach((item: any) => {
          const menu: any = { 'text': item.text, heading: false, submenu: item.menu, icon: item.icon, translate: item.translate };
          menus.push(menu);
        });
        return menus;
      }
      return result.info;
    });
    return promise;
  }
}
